package com.manager.controller;

import com.easyCode.feature.mybaits.CustomPage;
import com.easyCode.feature.result.ResponseResultAdvice;
import com.manager.entity.dto.ManagerDto;
import com.manager.entity.vo.ManagerVo;
import com.manager.service.IManagerService;
import com.validator.NotEmptyCheck;
import com.validator.RangeCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@Validated
@RestController
@RequestMapping("/manager")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "管理员请求")
public class ManagerController {
    private final IManagerService iManagerService;

    @ApiOperation("新增普通管理员")
    @PostMapping("/save")
    public Integer saveManager(@Validated ManagerVo managerVo) {
        return iManagerService.saveManager(managerVo);
    }

    @ApiOperation("根据id查询管理员")
    @GetMapping("/{id}")
    public ManagerDto getManagerById(@Positive(message = "参数不合法") @PathVariable("id") Integer id) {
        return iManagerService.getManagerById(id);
    }

    @ApiOperation("获取当前登录的管理员信息")
    @GetMapping("/self")
    public ManagerDto getSelf() {
        return iManagerService.getSelf();
    }

    @ApiOperation("改变管理员所属主校区")
    @GetMapping("/campus")
    public Boolean changeManagerCampus(@Positive(message = "参数不合法")@RequestParam("campusId")Integer campusId,
                                       @Positive(message = "参数不合法")@RequestParam("managerId")Integer managerId){
        return iManagerService.changeManagerCampus(campusId,managerId);
    }

    @ApiOperation("根据姓名查询管理员")
    @GetMapping("/name/{name}")
    public ManagerDto getManagerByName(@NotEmptyCheck(message = "姓名不能为空") @PathVariable("name") String name) {
        return iManagerService.getManagerByName(name);
    }

    @ApiOperation("分页批量查询管理员")
    @GetMapping("/list")
    public List<ManagerDto> listManager(@RequestParam(value = "example",required = false)String queryExample,CustomPage customPage) {
        return iManagerService.listManager(queryExample,customPage);
    }

    @ApiOperation("删除普通管理员")
    @DeleteMapping("/{id}")
    public void delManager(@Positive(message = "参数不合法") @PathVariable("id") Integer id) {
        iManagerService.delManagerById(id);
    }

    @ApiOperation("启用或者停用普通管理员")
    @PostMapping("/enableOrDisable/{id}")
    public boolean enableOrDisableManager(@Positive(message = "参数不合法")@PathVariable("id")Integer managerId,
                                          @RangeCheck (range = {0,1},message = "非法参数")@RequestParam("enable")Integer enable){
        return iManagerService.enableOrDisableManager(managerId,enable);
    }
}
